-- FeatureItemM
-- created by zouyb
-- 功能引导道具模块

module("FeatureItemM", package.seeall);

-- 配置表
local featureItemTable = {};
local ruleIdMap = {};

-- 所有子规则
local rules = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    -- 载入配置表
    local feature_item_CSV = get_feature_item_CSV();
    for i = 1, #feature_item_CSV do
        local id = feature_item_CSV[i].id;
        featureItemTable[id] = feature_item_CSV[i];
        ruleIdMap[feature_item_CSV[i].rule] = id;
    end

    feature_item_CSV = {};
end

-- 初始化
function init()
    -- 载入子模块
    if not _init then
        rules = LOAD_PATH2("game/logic/module/feature_item");
        loadCsv();
    end
end

-- 检索配置
function query(classId, path)
    local m = featureItemTable[classId];
    if nil == m then
        return nil;
    end
    if (path == "" or path == nil) then
        return m;
    else
        return m[path];
    end
end

-- 获取子规则
function getRule(ruleName)
    if rules[ruleName] == "TBL" then
        rules[ruleName] = LOAD_RUN("game/logic/module/feature_item/" .. ruleName);
    end

    return rules[ruleName];
end

-- 捡起引导物品
function onPickup(classId, pos)
    -- 奖励物品
    local bonusList = query(classId, "pickup_bonus");
    for _, arr in pairs(bonusList) do
        BonusM.doBonus(arr, "feature_item");
    end

    -- 执行后处理
    local ruleName = query(classId, "rule");
    local rule = getRule(ruleName);
    if rule then
        rule.onPickup();
    end

    -- 如果是从迷宫格子拾取的，直接
    if pos and DungeonM.getState() == DUNGEON_GAMING then
        -- 将格子设置为无效
        local grid = DungeonM.getGridByPos(pos);
        grid:changeState(GRID_STATE_DISABLE);
    end
    return bonusList;
end

-- 检查是否需要出现功能提醒窗口
function checkReminder(ruleName, callback)
    -- 检查是否需要提醒
    local flag = ME.user.dbase:query("features", ruleName, false);
    if flag ~= 0 then
        return false;
    end

    -- 显示提示消息
    local classId = ruleIdMap[ruleName];
    local remindDesc = query(classId, "remind_desc");

    callback(remindDesc);

    -- 提醒次数
    local remindTimes = query(classId, "remind_times");
    if remindTimes == 1 then
        markAsReminded(ruleName);
    end

    return true;
end

-- 标记功能已提醒
function markAsReminded(ruleName)
    local rule = getRule(ruleName);
    if rule then
        rule.onRemind();
    end

    -- 通知服务器该功能已经提醒过
    -- Operation.cmd_feature_remind(ruleName);

    -- 对迷宫有影响，通过迷宫的消息队列处理
    DungeonServiceM.addWaitSync("CMD_FEATURE_REMIND", { ["auth_id"] = os.time(), ["rule_name"] = ruleName }, true);

    EventMgr.fire(event.REMIND_FEATURE, {["ruleName"] = ruleName});
end

-- 功能是否激活
function isFeatureOpen(ruleName)
    if ALL_FEATURE_OPEN == 1 then
        return true;
    end

    -- 有子规则判断
    local rule = getRule(ruleName);
    if rule and rule.isFeatureOpen then
        return rule.isFeatureOpen();
    end

    return type(ME.user.dbase:query("features", ruleName, false)) == "number";
end

-- 功能是否需要提醒
function isFeatureNeedReminder(ruleName)
    return ME.user.dbase:query("features", ruleName, false) == 0;
end


